package gameam.business;

//Base question class
import java.util.Iterator;
import java.util.ArrayList;
import java.util.logging.*;
 
public class MCBaseQuestion
{
	private static final Logger LOGGER = Logger.getLogger(MCBaseQuestion.class.getName());

	private ArrayList<MCAnswer> _possibleAnswers; //contains all possible answers
	private String _questionString;
	
	//Default constructor
	public MCBaseQuestion(String question)
	{
		LOGGER.setLevel(Level.INFO);
		_possibleAnswers = new ArrayList<MCAnswer>();
		_questionString = question;
	}
	
	//Overloaded constructor
	public MCBaseQuestion(String question, ArrayList<MCAnswer> possibleAnswers)
	{
		LOGGER.setLevel(Level.INFO);
		_possibleAnswers = new ArrayList<MCAnswer>(possibleAnswers);
		_questionString = question;
	}
	
	//method to add answers
	public void AddAnswer(MCAnswer answer)
	{
		if(!_possibleAnswers.contains(answer))
		{
			_possibleAnswers.add(answer);
		}
		else
		{
			LOGGER.severe("AddAnswer: Attempted to add existing answer.");
		}
	}
	
	//method to remove answers
	public void RemoveAnswer(MCAnswer answer)
	{
		while(_possibleAnswers.contains(answer))
		{
			_possibleAnswers.remove(answer);
		}		
	}
	
	public int GetAnswerCount()
	{
		return _possibleAnswers.size();
	}
	
	public MCAnswer GetAnswerAtIndex(int index)
	{
		if ((index < 0) || (index >= _possibleAnswers.size()))
		{
			LOGGER.severe("GetAnswerAtIndex: index invalid.");
			return null;
		}
		
		return _possibleAnswers.get(index);
	}
	
	public String GetText()
	{
		return _questionString;
	}
	
	//calculates the max mark for the question (all answers with a positive weighting are chosen)
	public int MaxMarkForQuestion()
	{
		Iterator answerIterator = _possibleAnswers.iterator();
		
		int total = 0;
		
		while(answerIterator.hasNext())
		{
			int markForAnswer = ((MCAnswer)answerIterator.next()).MarkForAnswer();
			if(markForAnswer > 0)
			{
				total = total + markForAnswer;
			}
		}
		return total;
	}

	//calculates the min mark for the question (all answers with a negative weighting are chosen)
	public int MinMarkForQuestion()
	{
		Iterator answerIterator = _possibleAnswers.iterator();
		
		int total = 0;
		
		while(answerIterator.hasNext())
		{
			int markForAnswer = ((MCAnswer)answerIterator.next()).MarkForAnswer();
			if(markForAnswer < 0)
			{
				total = total + markForAnswer;
			}
		}
		return total;
	}
	
	//User clicks on an answer
	public int ToggleAnswer(MCAnswer answer)
	{
		int index = _possibleAnswers.indexOf(answer);
		if(index!= -1)
		{
			_possibleAnswers.get(index).ToggleSelection();
			return 0;
		}
		
		LOGGER.severe("ToggleAnswer: Attempted to toggle answer that does not exist.");
		return -1;
	}
	
	public int ToggleAnswer(int answerIndex)
	{
		if ((answerIndex >= 0) && (answerIndex < _possibleAnswers.size()))
		{
			_possibleAnswers.get(answerIndex).ToggleSelection();
			return 0;
		}
		
		LOGGER.severe("ToggleAnswer: Attempted to toggle answer that does not exist.");
		return -1;
	}
	
	//Calculates the mark the user achieves for the question. used by MCBaseTest's ProceedToNextQuestion() and GoToPreviousQuestion()
	public int AchievedMarkForQuestion()
	{
		Iterator answerIterator = _possibleAnswers.iterator();
		
		int total = 0;
		
		while(answerIterator.hasNext())
		{
			MCAnswer currentAnswer = (MCAnswer)answerIterator.next(); 
			if(currentAnswer.IsChosen())
			{
				total = total + currentAnswer.MarkForAnswer();
			}
		}
		if(total > this.MaxMarkForQuestion())
		{
			total = this.MaxMarkForQuestion();
		}
		
		if(total < this.MinMarkForQuestion())
		{
			total = this.MinMarkForQuestion();
		}
		
		return total;
	}
}